
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:zchope_student/apis.dart';
import 'package:zchope_student/beans/regionCityBean.dart';
import 'package:zchope_student/mine/mine.dart';
import 'package:zchope_student/beans/eventbean.dart';
import 'package:zchope_student/utils/mydio.dart';
import 'package:zchope_student/utils/utils.dart';

import '../const.dart';
import '../routes.dart';

class Course extends StatefulWidget {
  @override
  _CourseState createState() => _CourseState();
}

class _CourseState extends State<Course> with WidgetsBindingObserver {
//  InAppWebViewController webView;
  String _url = 'https://zcjpms.xiaoluxueche.com';
  var _mySubscription;
  SharedPreferences sharedPreferences;
  var signState = Const.UNSIGN;
  WebViewController _controller;
  @override
  void initState() {
    super.initState();
    _getSp();
    var code = Utils.getInstance().checkLogin();
    code.then((onValue) {
      signState = onValue;
      setState(() {
      });
      if (signState == Const.BIND) {
        var _cityName = Utils.getInstance().getCityName(
            sharedPreferences.get(Const.AREA_CODE));
        var _personNum = sharedPreferences.get(Const.PERSON_NUM);
        MyDio.getInstance().clearInstance();
        MyDio.getInstance(url: Const.BASEURL)
            .get('${Apis.getRegionByCity}$_cityName', (data) async {
          if (data['resultCode'] == 0) {
            RegionCityBean regionCityBean =
            RegionCityBean.fromJson(data['record']);
            _url =
            '${regionCityBean.trainingurl}api/queryTimePerson/$_personNum';
//            webView.loadUrl(url: _url);
            _controller.loadUrl(_url);
//            setState(() {
//              print('_url!!!!!:$_url');
//              webView.loadUrl(url: _url);
//            });
          }
        });
      }
    });
    //eventbus
    _mySubscription = eventBus.on<EventBean>().listen((data) {
      print('Course:${data.title}');
      var code = Utils.getInstance().checkLogin();
      code.then((onValue) {
        signState = onValue;
        setState(() {
        });
        if (signState == Const.BIND) {
          var _cityName = Utils.getInstance().getCityName(
              sharedPreferences.get(Const.AREA_CODE));
          var _personNum = sharedPreferences.get(Const.PERSON_NUM);
          MyDio.getInstance().clearInstance();
          MyDio.getInstance(url: Const.BASEURL)
              .get('${Apis.getRegionByCity}$_cityName', (data) async {
            if (data['resultCode'] == 0) {
              RegionCityBean regionCityBean =
              RegionCityBean.fromJson(data['record']);
              _url =
              '${regionCityBean.trainingurl}api/queryTimePerson/$_personNum';
//              webView.loadUrl(url: _url);
              _controller.loadUrl(_url);
            }
          });
        }
      });
    });
  }


  @override
  Widget build(BuildContext context) {
    print('_url?????:$_url');
    return Stack(
      children: <Widget>[
//        InAppWebView(
////          initialUrl: 'https://zcjpms.xiaoluxueche.com:8544/hbzcjp/api/queryTimePerson/410122196007020045',
//          initialUrl: _url,
//          initialHeaders: {},
//          initialOptions: InAppWebViewWidgetOptions(
//              inAppWebViewOptions: InAppWebViewOptions(
//                debuggingEnabled: true,
//              )
//          ),
//          onWebViewCreated: (InAppWebViewController controller) {
//            webView = controller;
//          },
//        ),

        WebView(
        initialUrl: _url,
//          initialUrl: 'https://zcjpms.xiaoluxueche.com:8543/hbzcjp/api/queryTimePerson/130527199711200867',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (WebViewController controller) {
          _controller = controller;
        },
        onPageFinished: (url) {
          _controller.evaluateJavascript('document.title').then((result) {
          });
        },
        javascriptChannels: <JavascriptChannel>[].toSet()),
        Offstage(
          offstage: signState == Const.UNBIND ? false : true,
          child: Container(
            color: Const.bar,
            width: MediaQuery
                .of(context)
                .size
                .width,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '尚未绑定身份证号',
                  style: TextStyle(fontSize: 18, color: Const.gold),
                ),
                SizedBox(
                  height: 20,
                ),
                GestureDetector(
                  onTap: () {
                    String route = '${Routes.userBind}';
                    Routes.router.navigateTo(context, route,
                        replace: false, transition: TransitionType.fadeIn);
                  },
                  child: BindButton(),
                ),
              ],
            ),

          ),
        ),
        Offstage(
          offstage: signState == Const.UNSIGN ? false : true,
          child: Container(
            width: MediaQuery
                .of(context)
                .size
                .width,
            color: Const.black,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '尚未登录',
                  style: TextStyle(fontSize: 18, color: Const.gold),
                ),
                SizedBox(
                  height: 20,
                ),
                GestureDetector(
                    onTap: () {
                      String route = '${Routes.loginMain}';
                      Routes.router.navigateTo(context, route,
                          replace: false, transition: TransitionType.fadeIn);
                    },
                    child: LoginButton()),
              ],
            ),
          ),
        )
      ],
    );
  }


  _getSp() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }

}
